package com.scy.java.loop;

public class ForDemo {
    public static void main(String[] args) {
        factorial();
        oddNumder();
        evenNumder();
        sumNumDer();
        parity();
        nestLoop();
    }
    //使用嵌套循环实现九九乘法表
    public static void nestLoop(){
        System.out.println("乘法口诀：");
        for (int i = 1; i <= 9; i++) {//行
            for (int j = 1; j <= i; j++) {//列
                System.out.print(j+"*"+i+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
    //计算1-100之间的奇数偶数和
    public static void parity(){
        int num = 1;
        int sumOdd = 0;
        int sumEven = 0;
        for (;;){
            if (num>100)break;
            if (num%2==0){
                sumEven+=num;
            }else {
                sumOdd+=num;
            }
            num++;
        }
        System.out.println("奇数和为："+sumOdd);
        System.out.println("偶数和为："+sumEven);
    }
    //计算1+4+7.。。+100的和
    public static void sumNumDer(){
        int sum =0;
        for (int i = 1; i <= 100; ) {
            sum+=i;
            i+=3;
        }
        System.out.println("1+4+7.。。+100的和为："+sum);
    }
    //计算1-100之间所有偶数的和
    public static void evenNumder(){

        int result = 0;
        for (int  num  = 1 ;; num++) {
            if (num >100)break;
//            判断是否为奇数
            if (num%2==0){
                result +=num;
            }

        }
        System.out.println("偶数和为："+result);
    }

    //计算1-100之间所有奇数的和
    public static void oddNumder(){
        int  num  = 1;
        int result = 0;
        for ( ;num < 101; num++) {
//            判断是否为奇数
            if (num%2!=0){
                result +=num;
            }
        }
        System.out.println("奇数和为："+result);
    }

    //for循环计算10的阶乘
    public static void factorial(){

        int result = 1;
        for (int i = 1; i <= 10; i++) {
            result *=i;
        }
        System.out.println("10的阶乘："+result);
    }
}
